//
//  HWDropdownMenu.m
//  黑马微博2期
//
//  Created by apple on 14-10-8.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "HWDropdownMenu.h"
@interface HWDropdownMenu()
/**
 *  将来用来显示具体内容的容器
 */
@property (nonatomic, weak) UIImageView *containerView;
@property (nonatomic, strong) UIButton *backgBtn;
@end

@implementation HWDropdownMenu

- (UIImageView *)containerView
{
    if (!_containerView) {
        // 添加一个灰色图片控件
        UIImageView *containerView = [[UIImageView alloc] init];
        containerView.image = [UIImage imageNamed:@"popover_background"];
        containerView.backgroundColor=[[UIColor lightGrayColor]colorWithAlphaComponent:0.35];
        containerView.userInteractionEnabled = YES; // 开启交互
        [self addSubview:containerView];
        self.containerView = containerView;
    }
    return _containerView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        // 清除颜色
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}


+ (instancetype)menu
{
    return [[self alloc] init];
}

- (void)setContent:(UIView *)content
{
    _content =content;
    
    // 调整内容的位置
//    content.x = 10;
//    content.y = 15;
    content.x = 0;
    content.y = 1;
    // 调整内容的宽度
//    content.width = self.containerView.width - 2 * content.x;
    
    // 设置灰色的高度
    self.containerView.height = CGRectGetMaxY(content.frame) ;
    // 设置灰色的宽度
    self.containerView.width = CGRectGetMaxX(content.frame) ;
    
    // 添加内容到灰色图片中
    [self.containerView addSubview:content];
}

- (void)setContentController:(UIViewController *)contentController
{
    _contentController = contentController;
    
    self.content = contentController.view;
}

/**
 *  显示
 */
- (void)showFrom:(UIView *)from
{
    // 1.获得最上面的窗口
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
   
    
    // 2.添加自己到窗口上
    [window addSubview:self];
    
    // 3.设置尺寸
    self.frame = window.bounds;
    
    // 4.调整灰色图片的位置
    // 默认情况下，frame是以父控件左上角为坐标原点
    // 转换坐标系
    CGRect newFrame = [from convertRect:from.bounds toView:window];
//    CGRect newFrame = [from.superview convertRect:from.frame toView:window];
    self.containerView.centerX = CGRectGetMidX(newFrame);
    self.containerView.y = CGRectGetMaxY(newFrame);
    
    //半透明蒙板
    UIButton *backgBtn=[[UIButton alloc]init];
    self.backgBtn=backgBtn;
    backgBtn.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.35];
    [backgBtn addTarget:self action:@selector(backActiom)];
    [window addSubview:backgBtn];
    [backgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containerView.mas_bottom);
        make.left.equalTo(window);
        make.right.equalTo(window);
        make.bottom.equalTo(window.mas_bottom);
    }];
    
}
#pragma -mark ————————————————————Action————————————————————

/**
 *  销毁
 */
- (void)dismiss
{
    [self removeFromSuperview];
    [self.backgBtn removeFromSuperview];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(self.dismisBlock){
        self.dismisBlock(@"1");
    }
    [self dismiss];
    
}
- (void)backActiom{
    if(self.dismisBlock){
        self.dismisBlock(@"1");
    }
    
     [self dismiss];
}
@end
